package cn.itsource.org.domain;

import cn.hutool.core.collection.CollectionUtil;
import cn.itsource.basic.domain.BaseTreeNode;
import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

//@Getter //相当于提供了get方法
//@Setter //相当于提供了set方法
@Data  //get set hashcode  equals toString
//@table 就是将表名与实体类名称做关系映射
@Table(name = "t_department")
public class Department extends BaseTreeNode<Department> {
    //主键
    //这个@id 是为了标识该字段对应数据库的主键id
    @Id
    private Long id;
    //说明
    @NotBlank(message = "请输入部门编号")
    private String sn;
    //名字
    @NotNull(message = "请输入部门名称")
    private String name;
    //部门层级路径 /2/5/10
    private String dirPath;
    //状态 （0：启动 -1 ：禁用）
    private Integer state;
    //没用
    private Long managerId;

    //部门经理
    private Employee manager;

    //没用
    private Long parentId;
    private List<Long> parentIds;

    //父部门
    private Department parent;

    //子部门组
    private List<Department> children = new ArrayList<>();

    public void init(){
        List<Long> parentIds = this.getParentIds();
        if (CollectionUtil.isEmpty(parentIds)) return;
        this.setState(1);
        this.setParentId(parentIds.get(parentIds.size()-1));
        String replace = parentIds.toString().
                replace("[", "").
                replace("]", "").
                replace(",", "/").
                replace(" ", "");
        this.setDirPath(replace);
    }
}
